package com.example.core.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.core.entity.DingdingUserPO;
import com.example.core.entity.DingdingUserSfPO;
import com.example.core.service.DingdingUserService;
import com.example.core.service.DingdingUserSfService;
import com.example.core.utils.Httputil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 考勤看板
 */
@Slf4j
@RestController
@RequestMapping(value = "/dingdingUser")
public class DingdingUserController {

    @Resource(name = "dingdingUserServiceImpl")
    private DingdingUserService dingdingUserService;

    @Resource(name = "dingdingUserSfServiceImpl")
    private DingdingUserSfService dingdingUserSfService;

    /**
     * 10.5.78.17:20087 生产
     * 10.5.1.11:28008 测试
     * 获取启明钉钉考勤
     * */
    @SneakyThrows
    @RequestMapping(value = "/getDingdingUser", method = RequestMethod.POST)
    public String  getDingdingUser() {
        /******************************************restTemplate**************************************/
        //生成一个设置了连接超时时间、请求超时时间、异常重试次数3次
        RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(1000000).setConnectTimeout(1000000).setSocketTimeout(3000000).build();
        HttpClientBuilder builder = HttpClientBuilder.create().setDefaultRequestConfig(config).setRetryHandler(new DefaultHttpRequestRetryHandler(1, false));
        HttpClient httpClient = builder.build();
        ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        /******************************************restTemplate**************************************/
        /*所有人员*/
        /*获取token*/
        JSONObject token = getAccessToken();
        String access_token = token.getString("access_token");
        List<String> deptList = new ArrayList<>();
        /****************************************************部门解析**********************************************************/
//        String depturl = "https://oapi.dingtalk.com/topapi/v2/department/listsubid?access_token="+access_token;//获取部门下人员信息
        String depturl = "http://10.5.78.17:20087/dingding/api";//获取部门下人员信息
        List<String> depidtList = new ArrayList<>();
        depidtList.add("576877157");//办公室 576877157
        depidtList.add("146760034");//党群工作部 146760034
        depidtList.add("146641559");//基础服务中心 146641559 其中去除数字化设备测试703710220、保安组714552127、物业组733403303
        depidtList.add("146732169");//纪检工作部 146732169
        depidtList.add("332846064");//党委巡察办 332846064
        depidtList.add("146789005");//人才服务中心 146789005
        depidtList.add("146537877");//战略规划及资本运营中心 146537877
        depidtList.add("146771054");//合规及法务部 146771054
        depidtList.add("146574934");//财务服务中心 146574934
        depidtList.add("146440974");//采购服务中心 146440974
        depidtList.add("576334449");//商品策划及产品管理部 576334449
        depidtList.add("146700258");//营销中心 146700258
        depidtList.add("576885616");//公共事业部 576885616
        depidtList.add("577205423");//创新业务部 577205423
        depidtList.add("577760178");//研发中心 577760178
        depidtList.add("594328486");//电子创新中心 594328486
//        depidtList.add("297800004");//数据中心
        depidtList.add("146694270");//菱电子公司 146694270
        depidtList.add("146653320");//安信子公司 146653320
        depidtList.add("146434948");//北京分公司 146434948
        depidtList.add("146835097");//天津通海子公司 146835097
        depidtList.add("146805115");//大连海通子公司 146805115
        depidtList.add("146716358");//成都春蓉子公司 146716358
        depidtList.add("146669469");//广州分公司 146669469
        depidtList.add("146755228");//青岛分公司146755228
//        depidtList.add("700691843");//专家顾问组
        for(int x=0;x<depidtList.size();x++){
            Map deptbody=new HashMap();
            Map requestbody=new HashMap();
            requestbody.put("dept_id",depidtList.get(x));//userId
            deptbody.put("path","/topapi/v2/department/listsubid");//请求钉钉接口地址 获取部门下人员信息
            deptbody.put("request",requestbody);//请求参数
            deptbody.put("type","POST");//类型
            //请求头
            HttpHeaders deptheaders = new HttpHeaders();
            deptheaders.add("access_token",access_token);
            HttpEntity<String> depthttpEntity = new HttpEntity(deptbody,deptheaders);
            ResponseEntity<Map> deptleaveBody = restTemplate.exchange(depturl, HttpMethod.POST, depthttpEntity, Map.class);//请假信息
            Map deptleavejsons = deptleaveBody.getBody();
            JSONObject deptleavejson = new JSONObject(deptleavejsons);
            JSONObject deptleavejson1 = deptleavejson.getJSONObject("result");
            JSONArray deptleavejson2 = deptleavejson1.getJSONArray("dept_id_list");
            for(int j=0;j<deptleavejson2.size();j++) {
                String YJ = String.valueOf(deptleavejson2.get(j));
                System.out.println(YJ);
                /****************************************************下级部门解析**********************************************************/
                //基础服务中心 146641559 其中去除数字化设备测试703710220、保安组714552127、物业组733403303
                if (!"703710220".equals(YJ)&&!"714552127".equals(YJ)&&!"733403303".equals(YJ)){
                    String deptYfurl = "http://10.5.78.17:20087/dingding/api";//获取部门下人员信息
                    Map deptYfbody=new HashMap();
                    Map requestYfbody=new HashMap();
                    requestYfbody.put("dept_id",YJ);//userId
                    deptYfbody.put("path","/topapi/v2/department/listsubid");//请求钉钉接口地址 获取部门下人员信息
                    deptYfbody.put("request",requestYfbody);//请求参数
                    deptYfbody.put("type","POST");//类型
                    //请求头
                    HttpHeaders deptYfheaders = new HttpHeaders();
                    deptYfheaders.add("access_token",access_token);
                    HttpEntity<String> deptYfhttpEntity = new HttpEntity(deptYfbody,deptYfheaders);
                    ResponseEntity<Map> deptYfleaveBody = restTemplate.exchange(deptYfurl, HttpMethod.POST, deptYfhttpEntity, Map.class);//请假信息
                    Map deptYfleavejsons = deptYfleaveBody.getBody();
                    JSONObject deptYfleavejson = new JSONObject(deptYfleavejsons);
                    JSONObject deptYfleavejson1 = deptYfleavejson.getJSONObject("result");
                    JSONArray deptYfleavejson2 = deptYfleavejson1.getJSONArray("dept_id_list");
                    for(int jj=0;jj<deptYfleavejson2.size();jj++) {
                        String detpid = deptYfleavejson2.get(jj).toString();
                        deptList.add(detpid);
                    }
                }
                /****************************************************下级部门解析**********************************************************/
            }
            /****************************************************部门解析**********************************************************/
        }
        //请求URL
        /****************************************************人员解析**********************************************************/
        String tokenurl = "http://10.5.78.17:20087/dingding/api";//获取部门下人员信息
        // 请求参数
        try {
            List<DingdingUserPO> dingdingUserList = new ArrayList<DingdingUserPO>();
            for(int d=0;d<deptList.size();d++){
                Map body=new HashMap();
                Map requestbody=new HashMap();
                requestbody.put("cursor",0);//
                requestbody.put("contain_access_limit","false");//
                requestbody.put("order_field","modify_desc");//
                requestbody.put("language","zh_CN");//
                requestbody.put("size",100);//
                requestbody.put("dept_id",deptList.get(d));//userId

                body.put("path","/topapi/v2/user/list");//请求钉钉接口地址
                body.put("request",requestbody);//请求参数
                body.put("type","POST");//类型
                //请求头
                HttpHeaders headers = new HttpHeaders();
                headers.add("access_token",access_token);
                HttpEntity<String> httpEntity = new HttpEntity(body,headers);
                ResponseEntity<Map> leaveBody = restTemplate.exchange(tokenurl, HttpMethod.POST, httpEntity, Map.class);//请假信息
                Map leavejsons = leaveBody.getBody();
                JSONObject leavejson = new JSONObject(leavejsons);
                JSONObject leavejson1 = leavejson.getJSONObject("result");
                JSONArray leavejson2 = leavejson1.getJSONArray("list");

                for(int i=0;i<leavejson2.size();i++) {
                    DingdingUserPO dingdingUser = new DingdingUserPO();
                    dingdingUser.setUsername(leavejson2.getJSONObject(i).getString("name"));//姓名
                    dingdingUser.setPhone(leavejson2.getJSONObject(i).getString("mobile"));//电话
                    dingdingUser.setUserid(leavejson2.getJSONObject(i).getString("userid"));//钉钉id
                    dingdingUser.setHrid(leavejson2.getJSONObject(i).getString("job_number"));//hrid
                    dingdingUserList.add(dingdingUser);
                }
            }
            dingdingUserService.save(dingdingUserList);
            /****************************************************人员解析**********************************************************/
        } catch (Exception e){
            System.out.println(e);
        }
        return "{ \"success\": 0\"}";
    }

    /*获取启明钉钉考勤*/
    @SneakyThrows
    @RequestMapping(value = "/getDingdingUserSf", method = RequestMethod.POST)
    public String  getDingdingUserSf() {
        /******************************************restTemplate**************************************/
        //生成一个设置了连接超时时间、请求超时时间、异常重试次数3次
        RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(1000000).setConnectTimeout(1000000).setSocketTimeout(3000000).build();
        HttpClientBuilder builder = HttpClientBuilder.create().setDefaultRequestConfig(config).setRetryHandler(new DefaultHttpRequestRetryHandler(1, false));
        HttpClient httpClient = builder.build();
        ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        /******************************************restTemplate**************************************/
        /*所有人员*/
        /*获取token*/
        JSONObject token = getAccessTokenSf();
        String access_token = token.getString("access_token");
        List<String> deptList = new ArrayList<>();
        /****************************************************部门解析**********************************************************/
        String depturl = "http://10.5.1.11:28008/dingding/api";//获取部门下人员信息
        List<String> depidtList = new ArrayList<>();
        depidtList.add("623707824");//研发中心
        depidtList.add("624238698");//创新业务部
        depidtList.add("624432593");//公共事业部
        for(int x=0;x<depidtList.size();x++){
            Map deptbody=new HashMap();
            Map requestbody=new HashMap();
            requestbody.put("dept_id",depidtList.get(x));//userId
            deptbody.put("path","/topapi/v2/department/listsubid");//请求钉钉接口地址
            deptbody.put("request",requestbody);//请求参数
            deptbody.put("type","POST");//类型
            //请求头
            HttpHeaders deptheaders = new HttpHeaders();
            deptheaders.add("access_token",access_token);
            HttpEntity<String> depthttpEntity = new HttpEntity(deptbody,deptheaders);
            ResponseEntity<Map> deptleaveBody = restTemplate.exchange(depturl, HttpMethod.POST, depthttpEntity, Map.class);//请假信息
            Map deptleavejsons = deptleaveBody.getBody();
            JSONObject deptleavejson = new JSONObject(deptleavejsons);
            JSONObject deptleavejson1 = deptleavejson.getJSONObject("result");
            JSONArray deptleavejson2 = deptleavejson1.getJSONArray("dept_id_list");
            for(int j=0;j<deptleavejson2.size();j++) {
                String YJ = String.valueOf(deptleavejson2.get(j));
                deptList.add(YJ);
                /****************************************************下级部门解析**********************************************************/
                String deptYfurl = "http://10.5.1.11:28008/dingding/api";//获取部门下人员信息
                Map deptYfbody=new HashMap();
                Map requestYfbody=new HashMap();
                requestYfbody.put("dept_id",YJ);//userId
                deptYfbody.put("path","/topapi/v2/department/listsubid");//请求钉钉接口地址
                deptYfbody.put("request",requestYfbody);//请求参数
                deptYfbody.put("type","POST");//类型
                //请求头
                HttpHeaders deptYfheaders = new HttpHeaders();
                deptYfheaders.add("access_token",access_token);
                HttpEntity<String> deptYfhttpEntity = new HttpEntity(deptYfbody,deptYfheaders);
                ResponseEntity<Map> deptYfleaveBody = restTemplate.exchange(deptYfurl, HttpMethod.POST, deptYfhttpEntity, Map.class);//请假信息
                Map deptYfleavejsons = deptYfleaveBody.getBody();
                JSONObject deptYfleavejson = new JSONObject(deptYfleavejsons);
                JSONObject deptYfleavejson1 = deptYfleavejson.getJSONObject("result");
                JSONArray deptYfleavejson2 = deptYfleavejson1.getJSONArray("dept_id_list");
                for(int jj=0;jj<deptYfleavejson2.size();jj++) {
                    String TJ = String.valueOf(deptYfleavejson2.get(jj));
                    deptList.add(TJ);
                    System.out.println(TJ);
                    /****************************************************下级部门解析**********************************************************/
                    String deptTjurl = "http://10.5.1.11:28008/dingding/api";//获取部门下人员信息
                    Map deptTjbody=new HashMap();
                    Map requestTjbody=new HashMap();
                    requestTjbody.put("dept_id",TJ);//userId
                    deptTjbody.put("path","/topapi/v2/department/listsubid");//请求钉钉接口地址
                    deptTjbody.put("request",requestTjbody);//请求参数
                    deptTjbody.put("type","POST");//类型
                    //请求头
                    HttpHeaders deptTjheaders = new HttpHeaders();
                    deptTjheaders.add("access_token",access_token);
                    HttpEntity<String> deptTjhttpEntity = new HttpEntity(deptTjbody,deptTjheaders);
                    ResponseEntity<Map> deptTjleaveBody = restTemplate.exchange(deptTjurl, HttpMethod.POST, deptTjhttpEntity, Map.class);//请假信息
                    Map deptTjleavejsons = deptTjleaveBody.getBody();
                    JSONObject deptTjleavejson = new JSONObject(deptTjleavejsons);
                    JSONObject deptTjleavejson1 = deptTjleavejson.getJSONObject("result");
                    JSONArray deptTjleavejson2 = deptTjleavejson1.getJSONArray("dept_id_list");
                    for(int jjj=0;jjj<deptTjleavejson2.size();jjj++) {
                        String detpid = deptTjleavejson2.get(jjj).toString();
                        deptList.add(detpid);
                    }
                    /****************************************************下级部门解析**********************************************************/
                }
                /****************************************************下级部门解析**********************************************************/
            }
            /****************************************************部门解析**********************************************************/
        }
        //请求URL
        /****************************************************人员解析**********************************************************/
        String tokenurl = "http://10.5.1.11:28008/dingding/api";//获取部门下人员信息
        // 请求参数
        try {
            List<DingdingUserSfPO> dingdingUserList = new ArrayList<DingdingUserSfPO>();
            for(int d=0;d<deptList.size();d++){
                Map body=new HashMap();
                Map requestbody=new HashMap();
                requestbody.put("cursor",0);//
                requestbody.put("contain_access_limit","false");//
                requestbody.put("order_field","modify_desc");//
                requestbody.put("language","zh_CN");//
                requestbody.put("size",100);//
                requestbody.put("dept_id",deptList.get(d));//userId
                body.put("path","/topapi/v2/user/list");//请求钉钉接口地址
                body.put("request",requestbody);//请求参数
                body.put("type","POST");//类型


                //请求头
                HttpHeaders headers = new HttpHeaders();
                headers.add("access_token",access_token);
                HttpEntity<String> httpEntity = new HttpEntity(body,headers);
                ResponseEntity<Map> leaveBody = restTemplate.exchange(tokenurl, HttpMethod.POST, httpEntity, Map.class);//请假信息
                Map leavejsons = leaveBody.getBody();
                JSONObject leavejson = new JSONObject(leavejsons);
                JSONObject leavejson1 = leavejson.getJSONObject("result");
                JSONArray leavejson2 = leavejson1.getJSONArray("list");

                for(int i=0;i<leavejson2.size();i++) {
                    DingdingUserSfPO dingdingUser = new DingdingUserSfPO();
                    dingdingUser.setUsername(leavejson2.getJSONObject(i).getString("name"));//姓名
                    dingdingUser.setPhone(leavejson2.getJSONObject(i).getString("mobile"));//电话
                    dingdingUser.setUserid(leavejson2.getJSONObject(i).getString("userid"));//钉钉id
                    dingdingUser.setHrid("");//hrid
                    dingdingUserList.add(dingdingUser);
                }
            }

            dingdingUserSfService.save(dingdingUserList);
            /****************************************************人员解析**********************************************************/
        }catch (Exception e){
            System.out.println(e);
        }
        return "{ \"success\": 0\"}";
    }
    private JSONObject getAccessToken() {
        JSONObject jso = new JSONObject();
        try {
            Map<String, Object> hearderMap = new HashMap<>();
            hearderMap.put("Content-Type", "application/x-www-form-urlencoded");
//            String url = "https://oapi.dingtalk.com/gettoken?appkey=dingnwvxr1mlbhpnhjjj&appsecret=asKCd8_7u5xLD_OtOOmIW3nvGqtJlNDLWGJTbsx_oZ0ufKaVTHSuUm1jg6Vr3ic0";
            String url = "http://10.5.78.17:20087/dingding/app/getToken?key=dingaa5kxmcq0p5zyndo&secret=ad65c9c9f3c1de067996b7e3d1e4b87d";
            jso = Httputil.sendHttpGet(url, null, hearderMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return jso;
    }
    /*所属组织：启明疫情防控管理*/
    private JSONObject getAccessTokenSf() {
        JSONObject jso = new JSONObject();
        try {
            Map<String, Object> hearderMap = new HashMap<>();
            hearderMap.put("Content-Type", "application/x-www-form-urlencoded");
//            String url = "https://oapi.dingtalk.com/gettoken?appkey=dingzlu1qyumitf5aqbk&appsecret=6kDJN83FeL1gc2FWShX61YNt5v7h6aZUx6nzpEQ5M3wfhP-n8HYSzgeR56egQxoD";
            String url = "http://10.5.1.11:28008/dingding/app/getToken?key=dingzlu1qyumitf5aqbk&secret=a9a31d1a85d0fb638e41aef29c17430c";
            jso = Httputil.sendHttpGet(url, null, hearderMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return jso;
    }
    /**
     * 字符转换函数
     *project
     * @param
     * @return output:如果字符串为null,返回为空,否则返回该字符串
     */
    public static String nullObject2String(Object s) {
        String str = "";
        try {
            str = s.toString();
        } catch (Exception e) {
            str = "";
        }
        return str;
    }


    /*获取启明钉钉考勤*/
    @SneakyThrows
    @RequestMapping(value = "/getDingdingUserqm", method = RequestMethod.POST)
    public String  getDingdingUserqm() {
        /******************************************restTemplate**************************************/
        //生成一个设置了连接超时时间、请求超时时间、异常重试次数3次
        RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(1000000).setConnectTimeout(1000000).setSocketTimeout(3000000).build();
        HttpClientBuilder builder = HttpClientBuilder.create().setDefaultRequestConfig(config).setRetryHandler(new DefaultHttpRequestRetryHandler(1, false));
        HttpClient httpClient = builder.build();
        ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        /******************************************restTemplate**************************************/
        /*所有人员*/
        /*获取token*/
        JSONObject token = getAccessTokenqm();
        String access_token = token.getString("access_token");
        List<String> deptList = new ArrayList<>();
        /****************************************************部门解析**********************************************************/
        String depturl = "https://oapi.dingtalk.com/topapi/v2/department/listsubid?access_token="+access_token;//获取部门下人员信息
        List<String> depidtList = new ArrayList<>();
        depidtList.add("576877157");//办公室 576877157
        depidtList.add("146760034");//党群工作部 146760034
        depidtList.add("146641559");//基础服务中心 146641559 其中去除数字化设备测试703710220、保安组714552127、物业组733403303
        depidtList.add("146732169");//纪检工作部 146732169
        depidtList.add("332846064");//党委巡察办 332846064
        depidtList.add("146789005");//人才服务中心 146789005
        depidtList.add("146537877");//战略规划及资本运营中心 146537877
        depidtList.add("146771054");//合规及法务部 146771054
        depidtList.add("146574934");//财务服务中心 146574934
        depidtList.add("146440974");//采购服务中心 146440974
        depidtList.add("576334449");//商品策划及产品管理部 576334449
        depidtList.add("146700258");//营销中心 146700258
        depidtList.add("576885616");//公共事业部 576885616
        depidtList.add("577205423");//创新业务部 577205423
        depidtList.add("577760178");//研发中心 577760178
        depidtList.add("594328486");//电子创新中心 594328486
//        depidtList.add("297800004");//数据中心
        depidtList.add("146694270");//菱电子公司 146694270
        depidtList.add("146653320");//安信子公司 146653320
        depidtList.add("146434948");//北京分公司 146434948
        depidtList.add("146835097");//天津通海子公司 146835097
        depidtList.add("146805115");//大连海通子公司 146805115
        depidtList.add("146716358");//成都春蓉子公司 146716358
        depidtList.add("146669469");//广州分公司 146669469
        depidtList.add("146755228");//青岛分公司146755228
//        depidtList.add("700691843");//专家顾问组
        for(int x=0;x<depidtList.size();x++){
            Map deptbody=new HashMap();
            deptbody.put("dept_id",depidtList.get(x));//userId
            //请求头
            HttpHeaders deptheaders = new HttpHeaders();
            HttpEntity<String> depthttpEntity = new HttpEntity(deptbody,deptheaders);
            ResponseEntity<Map> deptleaveBody = restTemplate.exchange(depturl, HttpMethod.POST, depthttpEntity, Map.class);//请假信息
            Map deptleavejsons = deptleaveBody.getBody();
            JSONObject deptleavejson = new JSONObject(deptleavejsons);
            JSONObject deptleavejson1 = deptleavejson.getJSONObject("result");
            JSONArray deptleavejson2 = deptleavejson1.getJSONArray("dept_id_list");
            for(int j=0;j<deptleavejson2.size();j++) {
                String YJ = String.valueOf(deptleavejson2.get(j));
                System.out.println(YJ);
                //基础服务中心 146641559 其中去除数字化设备测试703710220、保安组714552127、物业组733403303
                if (!"703710220".equals(YJ)&&!"714552127".equals(YJ)&&!"733403303".equals(YJ)){

                    /****************************************************下级部门解析**********************************************************/
                    String deptYfurl = "https://oapi.dingtalk.com/topapi/v2/department/listsubid?access_token="+access_token;//获取部门下人员信息
                    Map deptYfbody=new HashMap();
                    deptYfbody.put("dept_id",YJ);//userId
                    //请求头
                    HttpHeaders deptYfheaders = new HttpHeaders();
                    HttpEntity<String> deptYfhttpEntity = new HttpEntity(deptYfbody,deptYfheaders);
                    ResponseEntity<Map> deptYfleaveBody = restTemplate.exchange(deptYfurl, HttpMethod.POST, deptYfhttpEntity, Map.class);//请假信息
                    Map deptYfleavejsons = deptYfleaveBody.getBody();
                    JSONObject deptYfleavejson = new JSONObject(deptYfleavejsons);
                    JSONObject deptYfleavejson1 = deptYfleavejson.getJSONObject("result");
                    JSONArray deptYfleavejson2 = deptYfleavejson1.getJSONArray("dept_id_list");
                    for(int jj=0;jj<deptYfleavejson2.size();jj++) {
                        String detpid = deptYfleavejson2.get(jj).toString();
                        deptList.add(detpid);
                    }
                }
                /****************************************************下级部门解析**********************************************************/
            }
            /****************************************************部门解析**********************************************************/
        }
        //请求URL
        /****************************************************人员解析**********************************************************/
        String tokenurl = "https://oapi.dingtalk.com/topapi/v2/user/list?access_token="+access_token;//获取部门下人员信息
        // 请求参数
        try {
            List<DingdingUserPO> dingdingUserList = new ArrayList<DingdingUserPO>();
            for(int d=0;d<deptList.size();d++){
                Map body=new HashMap();
                body.put("cursor",0);//
                body.put("contain_access_limit","false");//
                body.put("order_field","modify_desc");//
                body.put("language","zh_CN");//
                body.put("size",100);//
                body.put("dept_id",deptList.get(d));//userId
                //请求头
                HttpHeaders headers = new HttpHeaders();
                HttpEntity<String> httpEntity = new HttpEntity(body,headers);
                ResponseEntity<Map> leaveBody = restTemplate.exchange(tokenurl, HttpMethod.POST, httpEntity, Map.class);//请假信息
                Map leavejsons = leaveBody.getBody();
                JSONObject leavejson = new JSONObject(leavejsons);
                JSONObject leavejson1 = leavejson.getJSONObject("result");
                JSONArray leavejson2 = leavejson1.getJSONArray("list");

                for(int i=0;i<leavejson2.size();i++) {
                    DingdingUserPO dingdingUser = new DingdingUserPO();
                    dingdingUser.setUsername(leavejson2.getJSONObject(i).getString("name"));//姓名
                    dingdingUser.setPhone(leavejson2.getJSONObject(i).getString(""));//电话
                    dingdingUser.setUserid(leavejson2.getJSONObject(i).getString("userid"));//钉钉id
                    dingdingUser.setHrid(leavejson2.getJSONObject(i).getString("job_number"));//hrid
                    dingdingUserList.add(dingdingUser);
                }
            }

            dingdingUserService.save(dingdingUserList);
            /****************************************************人员解析**********************************************************/
        }catch (Exception e){
            System.out.println(e);
        }
        return "{ \"success\": 0\"}";
    }


    private JSONObject getAccessTokenqm() {
        JSONObject jso = new JSONObject();
        try {
            Map<String, Object> hearderMap = new HashMap<>();
            hearderMap.put("Content-Type", "application/x-www-form-urlencoded");
//            String url = "https://oapi.dingtalk.com/gettoken?appkey=dingaa5kxmcq0p5zyndo&appsecret=HuakAGBm78fcQuOJcBOiF4E3bdV2X_CYSU0vXxIJjz23O-BthA8lY_-4hC2wxAel";
            String url = "https://oapi.dingtalk.com/gettoken?appkey=dingnwvxr1mlbhpnhjjj&appsecret=asKCd8_7u5xLD_OtOOmIW3nvGqtJlNDLWGJTbsx_oZ0ufKaVTHSuUm1jg6Vr3ic0";
            jso = Httputil.sendHttpGet(url, null, hearderMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return jso;
    }
}
